<?php
/**
 * Project: Recipe
 * File: ComponentAction.class.php
 * Author: Xnfy520@gmail.com
 * Date: 12-5-7
 * Time: 上午11:42
 */


class ComponentAction extends CommonAction{

	function index(){

		import("ORG.Util.Page");
		$component = M('Component');

		if(isset($_POST['search'])){
			if(!empty($_POST['key'])){
				switch($_POST['search']){
					case 1 :
						$datas['id'] = $_POST['key'];
						break;
					case 2:
						$datas['name'] = array("like","%{$_POST['key']}%");
						break;
					case 3 :
						$datas['alias'] = array('like',"%{$_POST['key']}%");
						break;
				}
				$total = $component->where($datas)->count();
				$Page = new Page($total, C('PAGENUM'));
				$show = $Page->show();
				$list = $component->where($datas)->limit($Page->firstRow.','.$Page->listRows)->order('id')->select();
			}else{
				$this->error('搜索内容不能为空', __APP__.'/Component/index');
			}
		}else{
			$total = $component->count();
			$Page = new Page($total, C('PAGENUM'));// 实例化分页类 传入总记录数和每页显示的记录数
			$show = $Page->show();// 分页显示输出
			$list = $component->limit($Page->firstRow.','.$Page->listRows)->order('id')->select();
		}

		$this->assign('list',$list);// 赋值数据集

		$this->assign('show',$show);// 赋值分页输出

		$this->display('index');
	}

	/**
	 * 指向添加组件界面
	 */
	function add(){
		$component = M('Component');

		$componentlist = array(
			'Custom',
			'RecipeCategory','RecipeArticle',
			'SpecialCategory','SpecialArticle',
			'SauceCategory','SauceArticle',
			'KitchenCategory','KitchenArticle',
		);

		$componentinfo = $component->field('model')->select();
//		dump($componentinfo);
		$counts = count($componentlist);
		foreach($componentinfo as $key=>$value){
			for($i=0; $i<$counts; $i++){
				if($componentlist[$i]==$value['model']){
					unset($componentlist[$i]);
				}
			}
		}
		if(empty($componentlist)){
			$this->error('没用可需要添加的组件');
		}
		$this->assign('componentlist', $componentlist);
		$actions = array('view');
		$this->assign('actions', $actions);
		$this->display();
	}

	/**
	 * 添加组件
	 */
	function insert(){
		$component = D('Component');
		$tmpdir = './Public/Content/tmp/';
		$srcdir = './Public/Content/Component/';
		if($data = $component->create()){
			if($component->add()){
//				if(!empty($data['image'])){
//					copy($tmpdir.$data['image'], $srcdir.$data['image']);
//					if($dirsrc = opendir($tmpdir)){
//						while($files = readdir($dirsrc)){
//							if($files!='.' && $files!='..'){
//								$pathfile = $tmpdir.$files;
//								if(is_file($pathfile)){
//									unlink($pathfile);
//								}
//							}
//						}
//					}
//				}
				$this->success('添加组件成功', __APP__.'/Component/index');
			}else{
				$this->error('添加组件失败');
			}
		}else{
			$this->error($component->getError(), __APP__.'/Component/add');
		}
	}

	/**
	 *组件封面上传
	 */
	function uploads(){
		import("ORG.Net.emit_upload");
		import("ORG.Util.emit_image");
		$upload = new upload(array("filepath"=>"./Public/Content/tmp","allowtype"=>array('gif','png','jpg'),"israndname"=>true,"maxsize"=>2000000));
		if($upload->uploadfiles('pic')){
			$img = new image("./Public/Content/tmp");
			$imgname =  $upload->getnewname();
			$img->thumb($imgname, 132, 75);
			echo $upload->getnewname();
		}else{
			echo $upload->geterrormsg();
		}
	}

	/**
	 * 指向修改组件界面
	 */
	function edit(){
		if(isset($_GET['id']) && !empty($_GET['id'])){

			$component = M('Component');
			$data = $component->find($_GET['id']);
			$this->assign('data', $data);

			$componentlist = array(
			'Custom',
			'RecipeCategory','RecipeArticle',
			'SpecialCategory','SpecialArticle',
			'SauceCategory','SauceArticle',
			'KitchenCategory','KitchenArticle',
			);

			$counts = count($componentlist);//需要分配组件数量

			$componentinfo = $component->field('model')->where('id!='.$_GET['id'])->select(); //获取除本组件外的所有组件

				foreach($componentinfo as $key=>$value){
					for($i=0; $i<$counts; $i++){
						if($componentlist[$i]==$value['model']){
							unset($componentlist[$i]);
						}
					}
				}
			$actions = array('view');
			$this->assign('actions', $actions);
			$this->assign('componentlist', $componentlist);
			$this->display();
		}else{
			$this->error('异常操作');
		}
	}

	/**
	 * 修改组件
	 */

	function update(){
		if(isset($_POST['id']) && !empty($_POST['id'])){
//			$tmpdir = './Public/Content/tmp/';
//			$srcdir = './Public/Content/Component/';
			$component = M('Component');
//			$componentinfo = $component->find($_POST['id']);
			if($data = $component->create()){
				if($component->save()){
//					if($data['image']!=$componentinfo['image']){
//						unlink($srcdir.$componentinfo['image']);
//						copy($tmpdir.$data['image'], $srcdir.$data['image']);
//						if($dirsrc = opendir($tmpdir)){
//							while($files = readdir($dirsrc)){
//								if($files!='.' && $files!='..'){
//									$pathfile = $tmpdir.$files;
//									if(is_file($pathfile)){
//										unlink($pathfile);
//									}
//								}
//							}
//						}
//					}
					$this->success('修改组件成功',  __APP__.'/Component/index');
				}else{
					$this->error('修改组件失败');
				}
			}else{
				$this->error($component->getError());
			}
		}else{
			$this->error('异常操作');
		}
	}

	/**
	 * 删除组件
	 */
	function del(){
		if(!empty($_POST['actions'])){
			$component = D('Component');
			foreach($_POST['actions'] as $key=>$value){
				$componentinfo = $component->relation(true)->find($value);
				if(empty($componentinfo['menuitem'])){
					$num = $component->delete($value);
				}else{
					$this->error('所选组件已被使得,不能被删除');
				}
			}
			if($num>0){
				$this->success('删除组件成功', __APP__.'/Component/index');
			}else{
				$this->error('删除组件失败');
			}
		}else{
			$this->error('请先选择组件');
		}
	}

}